﻿using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Easy.Common;

namespace Easy.DAL
{
    public class RepetitionDAL
    {
        private readonly SqlDatabase sqldb = new SqlDatabase(Config.TulingBuyCrmConnectString);
        public CustomResult doCheck(String username, String phone)
        {
            try
            {
                int num = 0;
                DbCommand dc = sqldb.GetSqlStringCommand("select *  from tlm_lead where DeletionStateCode=0 and (tlm_fullname=@username or tlm_mobilephone=@phone)");
                SqlParameter[] param ={
                                       new SqlParameter("@username",username),
                                       new SqlParameter("@phone",phone)
                                   };
                dc.Parameters.AddRange(param);
                DataSet ds = sqldb.ExecuteDataSet(dc);
                num += ds.Tables[0].Rows.Count;

                DbCommand comm = sqldb.GetSqlStringCommand("select *  from tlm_contact where DeletionStateCode=0 and (tlm_name=@username or tlm_mobilephone=@phone)");
                SqlParameter[] param2 = {
                                        new SqlParameter("@username",username),
                                        new SqlParameter("@phone",phone)
                                    };
                comm.Parameters.AddRange(param2);
                DataSet ds2 = sqldb.ExecuteDataSet(comm);
                num += ds2.Tables[0].Rows.Count;

                return new CustomResult()
                {
                    ResultCode = 0,
                    ResultData = num,
                    ResultMessage = num > 0 ? "存在重复" : "没有重复"
                };
            }
            catch (Exception)
            {
                return new CustomResult()
                {
                    ResultCode = 1,
                    ResultMessage = "查重发生错误"
                };
            }
            
        } 
    }
}